using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using Western;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{    
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
        
    }
    public AuthHeader Authentication;

    [SoapHeader("Authentication", Required = true)]
    [WebMethod]
    public DataSet ExecuteReader(string business, object[] parameters)
    {
        DataSet _object = null;
        if (!(Authentication.Username == "@dmin" && Authentication.Password == "nimd@"))
            new Exception("Access deny");

        string db_str = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
        DataAccess data_access = null;
        switch (business)
        {
            case "0000":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetDataByProcedure(parameters[0].ToString(), parameters[1].ToString());

                break;
            case "0103":
                data_access = new Western.Users(db_str);
                _object = ((Users)data_access).GetAllMenus(parameters[0].ToString());

                break;

            case "0401":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetCalendar(Convert.ToDateTime(parameters[0]),
                    parameters[1].ToString());

                break;
            case "0402":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetTranType(parameters[0].ToString());
                break;
            case "0403":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetFee(parameters[0].ToString());

                break;
            case "0106":
                data_access = new Western.Users(db_str);
                _object = ((Users)data_access).GetMenusByUsername(parameters[0].ToString(), parameters[1].ToString());

                break;

            case "0109":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetClientType();

                break;
            case "0111":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetSystemParameters();

                break;
            case "0112":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetReportParameters(parameters[0].ToString());

                break;
            case "0113":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetCompStruct();

                break;
            case "0114":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetReportInitial();

                break;

            case "0115":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetOptionList(Convert.ToInt16(parameters[0]));

                break;
            case "0108":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).GetReportSource(parameters[0].ToString(),
                  Convert.ToDateTime(  parameters[1]),Convert.ToDateTime( parameters[2]),
                    parameters[3].ToString(), parameters[4].ToString(),
                    parameters[5].ToString(), parameters[6].ToString(),
                    parameters[7].ToString(), parameters[8].ToString());
                break;
            case "0104":
                data_access = new Western.Users(db_str);
                _object = ((Users)data_access).GetAllUsers(parameters[0].ToString(),
                    parameters[1].ToString(),parameters[2].ToString(),
                    parameters[3].ToString(),parameters[4].ToString(),
                    Convert.ToByte(parameters[5]));
                break;
            case "0201":
                data_access = new Western.Securities(db_str);
                _object = ((Securities)data_access).GetSymbolList(parameters[0].ToString());
                break;
            case "0202":
                data_access = new Western.Securities(db_str);
                _object = ((Securities)data_access).GetSecurities(parameters[0].ToString(),
                    Convert.ToDateTime(parameters[1]));
                break;
            case "0203":
                data_access = new Western.Securities(db_str);
                _object = ((Securities)data_access).GetPriceBoard(parameters[0].ToString());
                break;
            case "0301":
                data_access = new Western.Orders(db_str);
                _object = ((Orders)data_access).GetTradingBookAcct(parameters[0].ToString(),
                    parameters[1].ToString(), parameters[2].ToString(),
                    parameters[3].ToString(), Convert.ToDateTime(parameters[4]),
                    Convert.ToDateTime(parameters[4]));
                break;
            case "0303":
                data_access = new Western.Orders(db_str);
                _object = ((Orders)data_access).GetOrderInQueueX(Convert.ToDateTime(parameters[0]),
                    parameters[1].ToString(), parameters[2].ToString(), 
                    parameters[3].ToString(), parameters[4].ToString());
                break;
            case "0304":
                data_access = new Western.Orders(db_str);
                _object = ((Orders)data_access).GetMatchedOrders(parameters[0].ToString(), parameters[1].ToString(),
                    parameters[2].ToString(), DateTime.Today, DateTime.Today);
                break;
            case "0307":
                data_access = new Western.Orders(db_str);
                _object = ((Orders)data_access).GetChangeAnCancel(DateTime.Today,
                    parameters[1].ToString(), parameters[2].ToString());
                break;
            case "0501":
                data_access = new Western.Cash(db_str);
                _object = ((Cash)data_access).GetCashBalance(parameters[0].ToString(),
                    parameters[1].ToString(), Convert.ToDateTime(parameters[2]));
                break;
            case "0503":
                data_access = new Western.Cash(db_str);
                _object = ((Cash)data_access).GetCashTrans(Convert.ToDateTime(parameters[0]),
                    Convert.ToDateTime(parameters[0]), parameters[2].ToString(), 
                    parameters[3].ToString(), parameters[4].ToString());
                break;
            case "0603":
                data_access = new Western.Custody(db_str);
                _object = ((Custody)data_access).GetCustodyTrans(Convert.ToDateTime(parameters[0]),
                    Convert.ToDateTime(parameters[0]), parameters[2].ToString(),
                    parameters[3].ToString(), parameters[4].ToString(), parameters[5].ToString());
                break;
            case "0604":
                data_access = new Western.Custody(db_str);
                _object = ((Custody)data_access).GetCustodySettlement(Convert.ToDateTime(parameters[0])
                    , parameters[1].ToString());
                break;
            case "0605":
                data_access = new Western.Custody(db_str);
                _object = ((Custody)data_access).GetStockBalSummary(Convert.ToDateTime(parameters[0]),
                    parameters[1].ToString());
                break;
            case "0601":
                data_access = new Western.Custody(db_str);
                _object = ((Custody)data_access).GetCustodyBalance(parameters[0].ToString(),
                    parameters[1].ToString(), Convert.ToDateTime(parameters[2]), parameters[3].ToString());
                break;
            case "0506":
                data_access = new Western.Cash(db_str);
                _object = ((Cash)data_access).GetCashPayable(parameters[0].ToString(),
                   Convert.ToDateTime(parameters[1]));
                break;
            case "0507":
                data_access = new Western.Cash(db_str);
                _object = ((Cash)data_access).GetCashSettlement(Convert.ToDateTime(parameters[0])
                    ,parameters[1].ToString());
                break;
            case "0508":
                data_access = new Western.Cash(db_str);
                _object = ((Cash)data_access).GetCashAdvance(Convert.ToDateTime(parameters[0])
                    , parameters[1].ToString());
                break;
            case "0701":
                data_access = new Western.Client(db_str);
                _object = ((Client)data_access).GetClientContract(parameters[0].ToString(),parameters[1].ToString());
                break;
            case "0702":
                data_access = new Western.Client(db_str);
                _object = ((Client)data_access).GetClientState(parameters[0].ToString(), parameters[1].ToString());
                break;
            case "0703":
                data_access = new Western.Client(db_str);
                _object = ((Client)data_access).GetAccountTransaction(Convert.ToDateTime(parameters[0]), 
                    Convert.ToDateTime(parameters[0]), parameters[1].ToString());
                break;
            default:
                break;
        }
        return _object;
    }

    [SoapHeader("Authentication", Required = true)]
    [WebMethod]
    public object ExecuteScalar(string business, object[] parameters)
    {
        object _object = null;
        if (!(Authentication.Username == "@dmin" && Authentication.Password == "nimd@"))
            new Exception("Access deny");

        string db_str = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
        DataAccess data_access = null;
        switch (business)
        {
            case "0000":
                data_access = new Western.Setup(db_str);
                _object = ((Setup)data_access).ModifyByProcedure(parameters[0].ToString(), parameters[1].ToString());

                break;
            case "0101":
                data_access = new Western.Users(db_str);
                _object = ((Users)data_access).GetRolesByUser(parameters[0].ToString(), parameters[1].ToString());

                break;
            case "0102":
                data_access = new Western.Users(db_str);
                _object = ((Users)data_access).CreateUsers(parameters[0].ToString(), parameters[1].ToString()
                    , Convert.ToInt16(parameters[2]), parameters[3].ToString(), parameters[4].ToString()
                    , parameters[5].ToString(), parameters[6].ToString(), Convert.ToInt16(parameters[7])
                    , parameters[9].ToString());

                break;
            case "0110":
                data_access = new Western.Users(db_str);
                _object = ((Users)data_access).ChangePassword(parameters[0].ToString(), 
                    parameters[1].ToString(), parameters[2].ToString());

                break;
            
            case "0504":
                data_access = new Western.Cash(db_str);
                _object = ((Cash)data_access).CreateCashTrans(Convert.ToDateTime(parameters[0]), parameters[1].ToString()
                    , parameters[2].ToString(), parameters[3].ToString(), Convert.ToDecimal(parameters[4])
                    , parameters[5].ToString(), parameters[6].ToString(), parameters[7].ToString()
                    , parameters[8].ToString(), Convert.ToDateTime(parameters[9]), Convert.ToDecimal(parameters[10]));

                break;
            case "0505":
                data_access = new Western.Cash(db_str);
                _object = ((Cash)data_access).ApproveCashTrans(Convert.ToDecimal(parameters[0]), 
                    parameters[1].ToString(), Convert.ToDateTime(parameters[2]));

                break;
            case "0302":
                data_access = new Western.Orders(db_str);
                _object = ((Orders)data_access).PlaceNewOrderX(Convert.ToDateTime(parameters[0]), parameters[1].ToString()
                    , parameters[2].ToString(), parameters[3].ToString(), parameters[4].ToString()
                    , Convert.ToDecimal(parameters[5]), parameters[6].ToString(), parameters[7].ToString());

                break;
            case "0305":
                data_access = new Western.Orders(db_str);
                _object = ((Orders)data_access).CancelOrderX(parameters[0].ToString(), parameters[1].ToString());

                break;
            case "0306":
                data_access = new Western.Orders(db_str);
                _object = ((Orders)data_access).ChangeOrderX(parameters[0].ToString(), 
                    Convert.ToDecimal(parameters[1]), parameters[2].ToString());

                break;
            default:
                break;
        }
        return _object;
    }
    [SoapHeader("Authentication", Required = true)]
    [WebMethod]
    public void ExecuteNonquery(string business, object[] parameters)
    {
        if (!(Authentication.Username == "@dmin" && Authentication.Password == "nimd@"))
            new Exception("Access deny");

        string db_str = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
        DataAccess data_access = null;
        switch (business)
        {
            case "0105":
                data_access = new Western.Users(db_str);
                ((Users)data_access).UpdateRight(Convert.ToInt32(parameters[0]),
                    Convert.ToInt32(parameters[1]), parameters[2].ToString());

                break;
            case "0602":
                data_access = new Western.Custody(db_str);
                ((Custody)data_access).CreateAccount(parameters[0].ToString(),parameters[1].ToString(),
                    Convert.ToDecimal(parameters[2]), Convert.ToDecimal(parameters[3]),
                    Convert.ToDecimal(parameters[4]), Convert.ToDecimal(parameters[5]), parameters[6].ToString());

                break;

            case "0502":
                data_access = new Western.Cash(db_str);
                ((Cash)data_access).CreateAccount(parameters[0].ToString(), parameters[1].ToString(),
                    Convert.ToDecimal(parameters[2]), Convert.ToDecimal(parameters[3]),
                    Convert.ToDecimal(parameters[4]), Convert.ToDecimal(parameters[5]), parameters[6].ToString());

                break;
            
            case "0403":
                data_access = new Western.Setup(db_str);
                ((Setup)data_access).CreateFee(parameters[0].ToString(), parameters[1].ToString()
                    , Convert.ToDouble(parameters[2]), Convert.ToDecimal(parameters[3])
                    , Convert.ToDecimal(parameters[4]), Convert.ToByte(parameters[5]), parameters[6].ToString()
                    ,Convert.ToDateTime( parameters[7]), parameters[8].ToString());

                break;
            case "0404":
                data_access = new Western.Setup(db_str);
                ((Setup)data_access).CreateCalendar(Convert.ToDateTime(parameters[0]), Convert.ToDateTime(parameters[1])
                    , parameters[2].ToString(), Convert.ToDateTime(parameters[3]), Convert.ToDateTime(parameters[4]));

                break;
            case "0104":
                data_access = new Western.Users(db_str);
                ((Users)data_access).CreateUsersX(parameters[0].ToString(), parameters[1].ToString()
                    , 0, parameters[3].ToString()
                    , parameters[4].ToString(), parameters[5].ToString(), parameters[6].ToString()
                    , Convert.ToInt16(parameters[7]), parameters[11].ToString(), Convert.ToDateTime(parameters[12]));

                break;
            case "0203":
                data_access = new Western.Securities(db_str);
                ((Securities)data_access).AddnewSecurities(parameters[0].ToString(), parameters[1].ToString()
                    , Convert.ToDecimal(parameters[2]), Convert.ToDecimal(parameters[3])
                    , parameters[4].ToString(), Convert.ToDateTime(parameters[5]), Convert.ToDecimal(parameters[6])
                    , Convert.ToDecimal(parameters[7]), Convert.ToDecimal(parameters[8]), parameters[9].ToString());

                break;
            default:
                break;
        }

    }
    [SoapHeader("Authentication", Required = true)]
    [WebMethod]
    public DataSet GetXmlData()
    {
        if (!(Authentication.Username == "@dmin" && Authentication.Password == "nimd@"))
            new Exception("Access deny");
        DataSet xml_data = new DataSet();
        xml_data.ReadXml(Server.MapPath("~") + "\\XMLGrid.xml");
        xml_data.ReadXml(Server.MapPath("~") + "\\XMLCombo.xml");
        return xml_data;
    }
}
